package com.junzhiit.nengji.api.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.exception.InvalidArgumentException;
import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.api.service.CouponsService;
import com.junzhiit.nengji.api.web.vo.CouponSearchVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/coupons")
public class CouponsController extends BaseController {
	
	@Autowired
	private CouponsService couponsService;
	
	@ApiOperation(value = "查询用户所有的优惠劵信息")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO findAllByUser(CouponSearchVO vo,Integer pageNumber, Integer maxPageRow ){
		if(vo == null){
			throw new InvalidArgumentException("couponSearch is required.");
		}
		AssertUtil.notNull(vo.getUserId(), "userId is required.");
		AssertUtil.notNull(vo.getStatus(), "status is required.");
		AssertUtil.notNull(vo.getSorting(), "sorting is required.");
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
		return couponsService.findAllByUser(vo,pageNumber,maxPageRow);
	}
	
	@ApiOperation(value = "查询所有用户可领取的(可使用,有效期内)优惠劵信息")
	@RequestMapping(value="/all",method = RequestMethod.GET)
	public ResultVO findAll(String userId,Integer pageNumber, Integer maxPageRow ){
		AssertUtil.notNull(pageNumber, "pageNumber is required.");
		AssertUtil.notNull(maxPageRow, "maxPageRow is required.");
		return couponsService.findAll(userId,pageNumber,maxPageRow);
	}
	
	@ApiOperation(value = "用户领取优惠劵")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO receive(String userId,String couponsId){
		AssertUtil.notNull(userId, "userId is required.");
		AssertUtil.notNull(couponsId, "couponsId is required.");
		return couponsService.insert(userId,couponsId);
	}
	
	@ApiOperation(value = "新人专属优惠劵")
	@RequestMapping(value="/exclusive", method = RequestMethod.GET)
	public ResultVO exclusive(String userId){
		AssertUtil.notNull(userId, "userId is required.");
		return couponsService.exclusive(userId);
	}
	
	@ApiOperation(value = "用户兑换优惠劵")
	@RequestMapping(value="/exchange",method=RequestMethod.POST)
	public ResultVO exchange(String promoCode,String userId){
		AssertUtil.notNull(promoCode, "promoCode is required.");
		AssertUtil.notNull(userId, "userId is required.");
		return couponsService.exchange(promoCode, userId);
	}
}
